clear
capture log close
set more off
set mem 500m


**** Columns 1-4 with robust SEs *****

use data_pt_pop_1891, clear

*** Fix manchester and salford
replace loc="manchester" if loc=="salford"
count
collapse (sum) pop, by(loc year)
count

sort loc
merge loc using data_town_cotton_shr_1851
tab _merge
keep if _merge==3
drop _merge



** LHS var
gen log_pop=log(pop)

sort loc year
gen gr_pop=log_pop-log_pop[_n-1] if loc==loc[_n-1]


** RHS var
gen cotton10=0
replace cotton10=1 if shr_cot>.1


gen p1861=0
replace p1861=1 if year==1861
gen p1871=0
replace p1871=1 if year==1871
gen p1881=0
replace p1881=1 if year==1881
gen p1891=0
replace p1891=1 if year==1891

gen p1861cot10=p1861*cotton10
gen p1871cot10=p1871*cotton10
gen p1881cot10=p1881*cotton10
gen p1891cot10=p1891*cotton10

gen p1871cotshr=p1871*shr_cot
gen p1881cotshr=p1881*shr_cot
gen p1891cotshr=p1891*shr_cot

*Identify textile cities
replace shr_wool=0 if shr_wool==.
replace shr_other_tex=0 if shr_other_tex==.
gen shr_otex = shr_wool + shr_other_tex
gen other_tex10=0
replace other_tex10=1 if shr_otex>.1 & shr_cot<.1

gen p1861otex10=p1861*other_tex10
gen p1871otex10=p1871*other_tex10
gen p1881otex10=p1881*other_tex10
gen p1891otex10=p1891*other_tex10

gen p1871otexshr=p1871*shr_otex
gen p1881otexshr=p1881*shr_otex
gen p1891otexshr=p1891*shr_otex


************ Regressions ***************

*** Panel regregssions
encode loc, gen(loc_code)
xtset loc_code year, yearly
drop if gr_pop==.

xi i.year

xtreg gr_pop p1861cot10  _I* if (year<1871), fe vce(robust)
est store FE0

xtreg gr_pop p1871cot10  _I* if (year>1851 & year<1881), fe vce(robust)
est store FE1

xtreg gr_pop p1871cot10  _I* if year<1881, fe vce(robust)
est store FE2

xtreg gr_pop p1871cot10 p1881cot10 p1891cot10 _I*, fe vce(robust)
est store FE3

xtreg gr_pop p1861cot10 p1871cot10 p1881cot10 p1891cot10 _I*, fe vce(robust)
est store FE4


outreg2 [FE0 FE1 FE2 FE3 ] using results_table_4_columns_1_to_4, tex replace see label  drop(_I*)



************* Table 4 columns 1-4 with spatial SEs ***********

use data_pt_pop_1891, clear

*** Fix manchester and salford
replace loc="manchester" if loc=="salford"
count
collapse (sum) pop, by(loc year)
count

sort loc

merge loc using data_town_cotton_shr_1851

tab _merge
keep if _merge==3
drop _merge


** LHS var
gen log_pop=log(pop)

sort loc year
gen gr_pop=log_pop-log_pop[_n-1] if loc==loc[_n-1]


** RHS var
gen cotton10=0
replace cotton10=1 if shr_cot>.1

gen p1861=0
replace p1861=1 if year==1861
gen p1871=0
replace p1871=1 if year==1871
gen p1881=0
replace p1881=1 if year==1881
gen p1891=0
replace p1891=1 if year==1891

gen p1861cot10=p1861*cotton10
gen p1871cot10=p1871*cotton10
gen p1881cot10=p1881*cotton10
gen p1891cot10=p1891*cotton10



***** Merge in lat and lon data
sort loc
merge loc using data_city_lat_lon_data
tab _merge
keep if _merge==3
drop _merge


***************
*** Panel regregssions
encode loc, gen(loc_code)
xtset loc_code year, yearly
drop if gr_pop==.
xi i.year i.loc
gen const=1


************ Regressions ***************

** Placebo 1841-1852 vs. 1851-1861
preserve
drop if year>1861

ols_spatial_HAC gr_pop p1861cot10 _Iyear_1861 _Iloc_2 _Iloc_3 _Iloc_4 _Iloc_5 _Iloc_6 _Iloc_7 _Iloc_8 _Iloc_9 _Iloc_10 _Iloc_11 _Iloc_12 _Iloc_13 _Iloc_14 _Iloc_15 _Iloc_16 _Iloc_17 _Iloc_18 _Iloc_19 _Iloc_20 _Iloc_21 _Iloc_22 _Iloc_23 _Iloc_24 _Iloc_25 _Iloc_26 _Iloc_27 _Iloc_28 _Iloc_29 _Iloc_30 _Iloc_31 _Iloc_32 _Iloc_33 _Iloc_34 _Iloc_35 _Iloc_36 _Iloc_37 _Iloc_38 _Iloc_39 _Iloc_40 _Iloc_41 _Iloc_42 _Iloc_43 _Iloc_44 _Iloc_45 _Iloc_46 const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store FE0

outreg2 [FE0] using results_table_4_column_1_spatial_SEs, tex replace see label  drop(_I*)
restore


** Baseline: 1851-1861 vs 1861-1871
preserve
keep if year>1851 & year<1881
ols_spatial_HAC gr_pop p1871cot10 _Iyear_1871 _Iloc_2 _Iloc_3 _Iloc_4 _Iloc_5 _Iloc_6 _Iloc_7 _Iloc_8 _Iloc_9 _Iloc_10 _Iloc_11 _Iloc_12 _Iloc_13 _Iloc_14 _Iloc_15 _Iloc_16 _Iloc_17 _Iloc_18 _Iloc_19 _Iloc_20 _Iloc_21 _Iloc_22 _Iloc_23 _Iloc_24 _Iloc_25 _Iloc_26 _Iloc_27 _Iloc_28 _Iloc_29 _Iloc_30 _Iloc_31 _Iloc_32 _Iloc_33 _Iloc_34 _Iloc_35 _Iloc_36 _Iloc_37 _Iloc_38 _Iloc_39 _Iloc_40 _Iloc_41 _Iloc_42 _Iloc_43 _Iloc_44 _Iloc_45 _Iloc_46 const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store FE0
outreg2 [FE0] using results_table_4_column_2_spatial_SEs, tex replace see label  drop(_I*)
restore

** Longer pre-period 1841-1861 vs. 1861-1871
preserve
keep if year<1881
ols_spatial_HAC gr_pop p1871cot10 _Iyear_1861 _Iyear_1871 _Iloc_2 _Iloc_3 _Iloc_4 _Iloc_5 _Iloc_6 _Iloc_7 _Iloc_8 _Iloc_9 _Iloc_10 _Iloc_11 _Iloc_12 _Iloc_13 _Iloc_14 _Iloc_15 _Iloc_16 _Iloc_17 _Iloc_18 _Iloc_19 _Iloc_20 _Iloc_21 _Iloc_22 _Iloc_23 _Iloc_24 _Iloc_25 _Iloc_26 _Iloc_27 _Iloc_28 _Iloc_29 _Iloc_30 _Iloc_31 _Iloc_32 _Iloc_33 _Iloc_34 _Iloc_35 _Iloc_36 _Iloc_37 _Iloc_38 _Iloc_39 _Iloc_40 _Iloc_41 _Iloc_42 _Iloc_43 _Iloc_44 _Iloc_45 _Iloc_46 const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store FE0
outreg2 [FE0] using results_table_4_column_3_spatial_SEs, tex replace see label  drop(_I*)
restore

** Long pre and post
preserve
ols_spatial_HAC gr_pop p1871cot10 p1881cot10 p1891cot10 _Iyear_1861 _Iyear_1871 _Iyear_1881 _Iyear_1891 _Iloc_2 _Iloc_3 _Iloc_4 _Iloc_5 _Iloc_6 _Iloc_7 _Iloc_8 _Iloc_9 _Iloc_10 _Iloc_11 _Iloc_12 _Iloc_13 _Iloc_14 _Iloc_15 _Iloc_16 _Iloc_17 _Iloc_18 _Iloc_19 _Iloc_20 _Iloc_21 _Iloc_22 _Iloc_23 _Iloc_24 _Iloc_25 _Iloc_26 _Iloc_27 _Iloc_28 _Iloc_29 _Iloc_30 _Iloc_31 _Iloc_32 _Iloc_33 _Iloc_34 _Iloc_35 _Iloc_36 _Iloc_37 _Iloc_38 _Iloc_39 _Iloc_40 _Iloc_41 _Iloc_42 _Iloc_43 _Iloc_44 _Iloc_45 _Iloc_46 const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store FE0
outreg2 [FE0] using results_table_4_column_4_spatial_SEs, tex replace see label  drop(_I*)
restore




*************** Table 4 Column 5 **************

use data_pt_pop_1901, clear

*** Fix manchester and salford
replace loc="manchester" if loc=="salford"
count
collapse (sum) pop, by(loc year)
count

sort loc
merge loc using data_town_cotton_shr_1851
tab _merge
keep if _merge==3
drop _merge



** LHS var
gen log_pop=log(pop)

sort loc year
gen gr_pop=log_pop-log_pop[_n-1] if loc==loc[_n-1]


** RHS var
gen cotton10=0
replace cotton10=1 if shr_cot>.1

gen p1861=0
replace p1861=1 if year==1861
gen p1871=0
replace p1871=1 if year==1871
gen p1881=0
replace p1881=1 if year==1881
gen p1891=0
replace p1891=1 if year==1891
gen p1901=0
replace p1901=1 if year==1901

gen p1871cot10=p1871*cotton10
gen p1881cot10=p1881*cotton10
gen p1891cot10=p1891*cotton10
gen p1901cot10=p1901*cotton10


************ Regressions ***************

*** Panel regregssions
encode loc, gen(loc_code)
xtset loc_code year, yearly
drop if gr_pop==.

xi i.year

xtreg gr_pop p1871cot10 p1881cot10 p1891cot10 p1901cot10 _I*, fe vce(robust)
est store REG

outreg2 [REG] using results_table_4_column_5, tex replace see label  drop(_I*)



********** Table 4 Columns 5 with Spatial SEs *************

use data_pt_pop_1901, clear

*** Fix manchester and salford
replace loc="manchester" if loc=="salford"
count
collapse (sum) pop, by(loc year)
count

sort loc

merge loc using data_town_cotton_shr_1851

tab _merge
keep if _merge==3
drop _merge


** LHS var
gen log_pop=log(pop)

sort loc year
gen gr_pop=log_pop-log_pop[_n-1] if loc==loc[_n-1]


** RHS var
gen cotton10=0
replace cotton10=1 if shr_cot>.1

gen p1861=0
replace p1861=1 if year==1861
gen p1871=0
replace p1871=1 if year==1871
gen p1881=0
replace p1881=1 if year==1881
gen p1891=0
replace p1891=1 if year==1891
gen p1901=0
replace p1901=1 if year==1901

gen p1871cot10=p1871*cotton10
gen p1881cot10=p1881*cotton10
gen p1891cot10=p1891*cotton10
gen p1901cot10=p1901*cotton10


***** Merge in lat and lon data
sort loc
merge loc using data_city_lat_lon_data
tab _merge
keep if _merge==3
drop _merge


**** prepare for regressions
encode loc, gen(loc_code)
xtset loc_code year, yearly
drop if gr_pop==.
xi i.year i.loc
gen const=1

************ Regressions ***************


ols_spatial_HAC gr_pop p1871cot10 p1881cot10 p1891cot10 p1901cot10  _Iyear_1871 _Iyear_1881 _Iyear_1891 _Iyear_1901 _Iloc* const, lat(lat) lon(lon) timevar(year) panelvar(loc_code) distcutoff(100) lagcutoff(0) star
est store FE0

outreg2 [FE0] using results_table_4_column_5_spatial_SEs, tex replace see label  drop(_I*)




*****


